package cn.cyh.queue_service.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

/**
 * WebSocket配置类
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    /**
     * 注册WebSocket处理器
     */
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 允许跨域
        registry.addHandler(callScreenWebSocketHandler(), "/ws/call-screen")
                .setAllowedOrigins("*");
        
        registry.addHandler(queueStatusWebSocketHandler(), "/ws/queue-status")
                .setAllowedOrigins("*");
    }

    /**
     * 注册ServerEndpointExporter，此配置会自动注册使用@ServerEndpoint注解的类
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
    
    /**
     * 叫号大屏WebSocket处理器
     */
    @Bean
    public cn.cyh.queue_service.websocket.CallScreenWebSocketHandler callScreenWebSocketHandler() {
        return new cn.cyh.queue_service.websocket.CallScreenWebSocketHandler();
    }
    
    /**
     * 队列状态WebSocket处理器
     */
    @Bean
    public cn.cyh.queue_service.websocket.QueueStatusWebSocketHandler queueStatusWebSocketHandler() {
        return new cn.cyh.queue_service.websocket.QueueStatusWebSocketHandler();
    }
} 